package logic

import (
	"context"
	"userrpc/userrpcclient"

	"userapi/internal/svc"
	"userapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MobileLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MobileLogic {
	return &MobileLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MobileLogic) Mobile(req *types.MobileRequest) (resp *types.MobileResponse, err error) {
	// todo: add your logic here and delete this line
	mobile := req.Mobile
	code := l.svcCtx.Redis.Get(context.Background(), "code:"+mobile).Val()
	res, err := l.svcCtx.UserRpc.Mobile(context.Background(), &userrpcclient.MobileRequest{
		Mobile: mobile,
	})
	if err != nil {
		return &types.MobileResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
			Data: "",
		}, nil
	}
	if res.Code != 200 {
		return &types.MobileResponse{
			Code: int(res.Code),
			Msg:  res.Msg,
			Data: "",
		}, nil
	}
	return &types.MobileResponse{
		Code: int(res.Code),
		Msg:  res.Msg,
		Data: code,
	}, nil
}
